package pl.freetimetool.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pl.freetimetool.common.AccountRoles;
import pl.freetimetool.domain.Account;
import pl.freetimetool.domain.HolidayRequest;
import pl.freetimetool.service.AccountService;
import pl.freetimetool.service.HolidayRequestService;
import pl.freetimetool.service.HolidaysInPolandService;

@Controller
public class HolidayRequestController {

	@Autowired
	HolidayRequestService holidayRequestService;
	
	@Autowired
	AccountService accountService;
	
	@Autowired
	HolidaysInPolandService holidaysInPolandService;

	@RequestMapping(value = "/holidayrequest/add", method = RequestMethod.GET)
	public ModelAndView add() {
		final ModelAndView mav = new ModelAndView("/holidayrequest/add");
		final HolidayRequest hr = new HolidayRequest();
		mav.addObject("holidayRequest", hr);
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/add", method = RequestMethod.POST)
	public ModelAndView add(@Valid final HolidayRequest holidayRequest, final BindingResult result) {
		final ModelAndView mav = new ModelAndView("/holidayrequest/add");

		if (result.hasErrors()) {
			mav.addObject("holidayRequest", holidayRequest);
			return mav;
		}

		final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		final Account account = this.accountService.getAccount(auth.getName());

		holidayRequest.setAccount(account);
		this.holidayRequestService.addHolidayRequest(holidayRequest);

		mav.setViewName("redirect:/holidayrequest/showDetails/" + holidayRequest.getId());
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/showDetails/{id}", method = RequestMethod.GET)
	public ModelAndView details(@PathVariable("id") final Integer id) {
		final ModelAndView mav = new ModelAndView("holidayrequest/showDetails");
		mav.addObject("holidayRequest", this.holidayRequestService.getHolidayRequest(id));

		return mav;
	}

	@RequestMapping(value = "/holidayrequest/delete", method = RequestMethod.GET)
	public ModelAndView delete() {
		final ModelAndView mav = new ModelAndView("/holidayrequest/delete");
		mav.addObject("id", new Integer(0));
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/delete", method = RequestMethod.POST)
	public ModelAndView delete(@Valid final Integer id) {
		final ModelAndView mav = new ModelAndView("holidayrequest/delete");

		this.holidayRequestService.removeHolidayRequest(id);

		mav.setViewName("redirect://holidayrequest/showDetails/" + id);
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteById(@PathVariable("id") final Integer id) {
		final ModelAndView mav = new ModelAndView("holidayrequest/delete");

		final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		final Account currentAccount = this.accountService.getAccount(auth.getName());
		final Account holidayOwner = this.holidayRequestService.getHolidayRequest(id).getAccount();

		if (currentAccount.equals(holidayOwner) || currentAccount.getAccountRole().contains(AccountRoles.ADMIN.toString())) {
			this.holidayRequestService.removeHolidayRequest(id);
		}

		mav.setViewName("redirect://worker/workerPanel");
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/list", method = RequestMethod.GET)
	public ModelAndView listHolidaysForCurrentUser() {
		final ModelAndView mav = new ModelAndView("holidayrequest/list");

		final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		final Account account = this.accountService.getAccount(auth.getName());
		final Collection<HolidayRequest> holidayList = this.holidayRequestService.getHolidayRequestsList(account);
		mav.addObject("holidayList", holidayList);
		return mav;
	}

	@RequestMapping(value = "/holidayrequest/showAll", method = RequestMethod.GET)
	public ModelAndView showAll() {
		final ModelAndView mav = new ModelAndView("holidayrequest/showAllRequests");
		mav.addObject("holidayRequestRowsList", this.holidayRequestService.findAllHolidayRequests());
		return mav;
	}
	
	@RequestMapping(value = "/holidayrequest/workingDays", method = RequestMethod.GET)
	public @ResponseBody String getWorkingDays(@RequestParam String startDate, @RequestParam String endDate){
		
		DateFormat formatter = new SimpleDateFormat("dd-MM-yy");
		String workingDays="";
		try {
			Date formatedStartDate = formatter.parse(startDate);
			Date formatedEndDate = formatter.parse(endDate);
			workingDays = Integer.toString(holidaysInPolandService.workingDaysBetween(formatedStartDate, formatedEndDate));
			
		} catch (ParseException e) {
			workingDays = "Format daty jest nie wlasciwy";
		}
		catch(IllegalArgumentException e){
			workingDays = e.getMessage();
		}

		
		return workingDays;
	}
}
